package cc.bookcloud.tool.ocr.common.parsetext.test;

import java.util.ArrayList;
import java.util.List;

/**
 * @Desc: @todo
 * @Version: 2.0
 * @Author: John Goo
 * @Date: 2024/12/04 14:26
 **/
public class TicketParser {
    public static List<TicketItem> parseTicketData(String data) {
        List<TicketItem> ticketItems = new ArrayList<>();
        String[] items = data.split("]], \\[\\[\\[");

        for (String item : items) {
            // Remove leading and trailing brackets
            item = item.replaceAll("[\\[\\]]", "").trim();

            // Split the item into coordinates and text/confidence
            String[] parts = item.split("\\], \\[\"");
            String[] coordParts = parts[0].split(", ");
            String textAndConfidence = parts[1];

            // Parse coordinates
            double[][] coordinates = new double[4][2];
            for (int i = 0; i < 4; i++) {
                String[] point = coordParts[i].split(", ");
                coordinates[i][0] = Double.parseDouble(point[0]);
                coordinates[i][1] = Double.parseDouble(point[1]);
            }

            // Parse text and confidence
            String[] textConfidenceParts = textAndConfidence.split("\", ");
            String text = textConfidenceParts[0];
            double confidence = Double.parseDouble(textConfidenceParts[1]);

            // Create and add the TicketItem
            ticketItems.add(new TicketItem(coordinates, text, confidence));
        }

        return ticketItems;
    }
}
